import 'package:flutter_enjoy_plus/constants/index.dart';
import 'package:shared_preferences/shared_preferences.dart';

class TokenManager {
  String? _token;
  _getInstance() async {
    final SharedPreferences pre = await SharedPreferences.getInstance();
    return pre;
  }

  Future<void> setToken(String token) async {
    final SharedPreferences pre = await _getInstance();
    pre.setString(GlobalConstants.TOKEN_KEY, token);
    _token = token;
  }

  // Future<String> getToken() async {
  //   final SharedPreferences pre = await _getInstance();
  //   return pre.getString(GlobalConstants.TOKEN_KEY) ?? '';
  // }
  initToken() async {
    final SharedPreferences pre = await _getInstance();
    _token = pre.getString(GlobalConstants.TOKEN_KEY);
  }

  String getToken() {
    return _token ?? '';
  }

  Future<void> removeToken() async {
    final SharedPreferences pre = await _getInstance();
    pre.remove(GlobalConstants.TOKEN_KEY);
    //清空token
    _token = '';
  }
}

//单例模式
final TokenManager tokenManager = TokenManager();
